package com.example._003configtest.condition;

import com.example._003configtest.annotation.ConditionalCustom;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;

import java.util.Map;


public class MyCondition implements Condition {

    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        //获取@ConditionalCustom注解的全部属性,其中ConditionalCustom是自定义的注解
        Map<String, Object> annotationAttributes = metadata.getAnnotationAttributes(ConditionalCustom.class.getName());
        //获取注解的value属性值
        String[] vals = (String[]) annotationAttributes.get("value");
        Environment env = context.getEnvironment();
        //遍历每个属性值
        for (String val : vals) {
            //如果某个属性值对应的配置属性不存在，则返回false
            if(env.getProperty(val.toString())== null){
                return false;
            }
        }
        return true;
    }
}
